<?php
/*
Copyright 2008, Nathalia


Author:
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)

*/
class Editar extends TPage
{
    public $pasta = "files/artigos/";
    public $link = "";
    public $ArtigoID;

	public function onInit($param)
	{
		parent::onInit($param);

        $this->congressoList->DataTextField='Nome';
		$this->congressoList->DataValueField='CongressoID';
		$this->congressoList->DataSource=CongressoRecord::finder()->findAll();
		$this->congressoList->dataBind();

        $this->autoresList->DataTextField='Nome';
		$this->autoresList->DataValueField='UsuarioID';
		$this->autoresList->DataSource=UserRecord::finder()->findAll();
		$this->autoresList->dataBind();

        $artigoRecord              = $this->ArtigoRecord;
        $this->ArtigoID            = $artigoRecord ->ArtigoID;

        $this->getAutores();

		if(!$this->IsPostBack)  // if the page is initially requested
		{
            $this->Titulo->Text        = $artigoRecord ->Titulo;          
            $this->Resumo->Text        = $artigoRecord ->Resumo;          
            $this->Abstract->Text      = $artigoRecord ->Abstract;
            $this->PalavraChave1->Text = $artigoRecord ->PalavraChave1;
            $this->PalavraChave2->Text = $artigoRecord ->PalavraChave2;
            $this->PalavraChave3->Text = $artigoRecord ->PalavraChave3;
            $this->Status->Text        = $artigoRecord ->Status;   
            $this->congressoList->setSelectedValue($artigoRecord->CongressoID);      
		}

        $this->link = $this->pasta."artigo".$this->ArtigoID.".pdf";
        $this->panelb->visible = false;
        $this->atualizarArquivos();
	}

	public function editarArtigo($sender,$param)
	{
		if($this->IsValid)  // when all validations succeed
		{
            $artigoRecord                = $this->ArtigoRecord;
            $artigoRecord->Titulo        = $this->Titulo->Text; 
            $artigoRecord->Resumo        = $this->Resumo->Text; 
            $artigoRecord->Abstract      = $this->Abstract->Text; 
            $artigoRecord->PalavraChave1 = $this->PalavraChave1->Text; 
            $artigoRecord->PalavraChave2 = $this->PalavraChave2->Text;
            $artigoRecord->PalavraChave3 = $this->PalavraChave3->Text;
            $artigoRecord->Status        = $this->Status->Text;   
            $artigoRecord->DataEnvio     = date('Y-m-d H-i-s');  
            $artigoRecord->CongressoID   = $this->congressoList->getSelectedValue();        

			// saves to the database via Active Record mechanism
			$artigoRecord ->save();

			// redirects the browser to the homepage
			$this->Response->redirect("./index.php?page=Artigos.Listar");
		}
	}

	protected function getArtigoRecord ()
	{
		// if the 'username' GET var is not empty and the current user
		// is an administrator, we use the GET var value instead.
		if($this->Request['artigoID'] !== null)
			$artigoID = $this->Request['artigoID'];

		// use Active Record to look for the specified username
		$artigoRecord = ArtigoRecord::finder()->findByArtigoID($artigoID);
		if(!($artigoRecord  instanceof ArtigoRecord ))
			throw new THttpException(500,'ArtigoID is invalid.');
		return $artigoRecord ;
	}

    public function getAutores()
    {
        // fetches all data account information
        $criteria = new TActiveRecordCriteria;
        $criteria->Condition = 'ArtigoID = '.$this->ArtigoID;
        $artigos = ArtigoRecord::finder()->withAutores()->findAll($criteria);

        $this->UserGrid->DataSource = $artigos[0]->autores;
        $this->UserGrid->dataBind();
    }

    public function adicionarClicked($sender,$param)
    {
        if($param instanceof TCommandEventParameter and $param->CommandName == "adicionar")
        {
            //verifica se já está adicionado
            $criteria = new TActiveRecordCriteria;
            $criteria->Condition = 'ArtigoID = '.$this->ArtigoID.' AND AutorID = '.$this->autoresList->getSelectedValue();

            if( ArtigoAutorRecord::finder()->findAll($criteria) == null )
            {
                $artigoAutorRecord           = new ArtigoAutorRecord;
                $artigoAutorRecord->AutorID  = $this->autoresList->getSelectedValue(); 
                $artigoAutorRecord->ArtigoID = $this->ArtigoID; 
                $artigoAutorRecord->save();
            }

            $this->getAutores();
        }
    }
    
    public function deleteButtonClicked($sender,$param)
    {     
        // obtains the datagrid item that contains the clicked delete button
        $item = $param->Item;
        // obtains the primary key corresponding to the datagrid item
        $autorID = $this->UserGrid->DataKeys[$item->ItemIndex];

        $criteria = new TActiveRecordCriteria;
        $criteria->Condition = 'AutorID = '.$autorID.' and ArtigoID = '.$this->ArtigoID;
        
        // deletes the user record with the specified username primary key
        ArtigoAutorRecord::finder()->deleteAll($criteria);

        $this->getAutores();
    }

    
    public function fileUploaded($sender,$param)
	{
		if($sender->getHasFile())
		{
            if($sender->id == "artigo")
            {
                $nome = $this->pasta."artigo".$this->ArtigoID.".pdf";
            }
			$salvou = $sender->saveAs($nome);	
		}

        $this->atualizarArquivos();

	}

    public function removerClicked($sender,$param)
    {
        if($param instanceof TCommandEventParameter and $param->CommandName == "remover")

            switch($param->CommandParameter)
            {
                case 1:
                    if(file_exists($this->pasta."artigo".$this->ArtigoID.".pdf"))
                    {
                        unlink($this->pasta."artigo".$this->ArtigoID.".pdf");
                    }
                    break;
            }
            $this->atualizarArquivos();

    }

    public function atualizarArquivos()
    {

        if(file_exists($this->pasta."artigo".$this->ArtigoID.".pdf"))
        {                
            $this->panela->visible = false;
            $this->panelb->visible = true;
            $this->tamanho->Text = round(filesize($this->pasta."artigo".$this->ArtigoID.".pdf")/1024,1);
            $this->artName->Text = "artigo".$this->ArtigoID.".pdf";
        }
        else 
        {
            $this->panela->visible = true;
            $this->panelb->visible = false;
        }


    }

}
?>
